package pl.ydp.components.mathcell.mathrecognizer.characters
{
	import mx.messaging.channels.StreamingAMFChannel;
	
	public class Character
	{
		public function Character(_char:String = "")
		{
			if (_char.length > 1){
				throw (new CharacterError());		
			}
			
			synonymCodes = new Array;
			
			if (_char.charCodeAt(0) >= String("0").charCodeAt(0)  &&  _char.charCodeAt(0) <= String("9").charCodeAt(0)){
				type = CharacterType.DIGIT;
				code = _char.charCodeAt();
				symbol = _char;
			} else if ( _char == "+"  ||  _char == "-"  ||
						_char == "*"  ||  
						_char == "·"  ||  _char == "×"  ||  //"·"• 
						_char == "/"  ||
						_char == ":"  ||  _char == "÷"  ||
						_char == "."  || 
						_char == "="  ||  _char == "≠"  ||
						_char == "<"  ||  _char == ">"  ||
						//_char == "["  ||  _char == "]"  ||
						_char == "("  ||  _char == ")"  ||
						_char == "≤"  ||  _char == "≥"  ||
						_char == "."  ||  _char == ","||  
						_char == "≈"){
				type = CharacterType.OPERATOR;
				code = _char.charCodeAt();
				symbol = _char;
				
				createSynonymsArray(code);
				
			} else if ( _char == "!"  ||  _char == "[" ||  _char == "]"  ) {
				
				type = CharacterType.OPERATOR;
				
				if (_char == "!"){
					code = String("≠").charCodeAt();
				} else if (_char == "["){
					code = String("≤").charCodeAt();
				} else if (_char == "]"){
					code = String("≥").charCodeAt();
				}
				
				symbol = _char;
				
			} else if(_char == "|") {
				type = CharacterType.SPECIAL;
				code = _char.charCodeAt();
				symbol = _char;
			} else {
				
				type = CharacterType.UNKNOWN;
				code = -1;
				symbol = "";
				
			}
			
		}
		
		//----------------------------------------------------------------
		
		public function fromCode(c:Number):void{
			
			code = c;
			
			symbol = String.fromCharCode(code);
			
			if (c >= ZERO  &&  c <= NINE){
				type = CharacterType.DIGIT;
			} else if (c == PLUS  ||  c == MINUS  ||  
						c == MULTIPLY  ||  c == MULTIPLY_A  ||  c == MULTIPLY_X  ||  
						c == DIVIDE ||  c == DIVIDE_S ||  c == DIVIDE_G ||
						c == EQUALS  ||  c == NOT_EQUALS  ||  
						c == LESS  ||  c == GREATER  ||  
						c == LESS_OR_EQUALS  ||  c == GREATER_OR_EQUALS  ||  
						c == DOT  ||  
						c == BRACKET_OPEN  ||  c == BRACKET_CLOSE){
				type = CharacterType.OPERATOR;
				createSynonymsArray(code);
			} else if (c == SPECIAL_FRACTION){
				type = CharacterType.SPECIAL
			} else {
				code = -1;
				type = CharacterType.UNKNOWN;
				symbol = "";
			}
			
		}
		
		//----------------------------------------------------------------
		
		private function createSynonymsArray(c:Number):void{
			
			synonymCodes = new Array;
			
			if (c == MULTIPLY_A){
				synonymCodes.push(MULTIPLY, MULTIPLY_X);
			} else if (c == MULTIPLY){
				synonymCodes.push(MULTIPLY_A, MULTIPLY_X);
			} else if (c == MULTIPLY_X){
				synonymCodes.push(MULTIPLY_A, MULTIPLY);
			}
			
			if (c == DIVIDE_S){
				synonymCodes.push(DIVIDE, DIVIDE_G);
			} else if (c == DIVIDE){
				synonymCodes.push(DIVIDE_S, DIVIDE_G);
			} else if (c == DIVIDE_G){
				synonymCodes.push(DIVIDE, DIVIDE_S);
			}
			
		} 
		
		//----------------------------------------------------------------
		
		public var code:Number;
		
		public var symbol:String;
		
		public var type:Number;
		
		public var synonymCodes:Array;
		
		public static const ZERO:Number		= String("0").charCodeAt();
		public static const ONE:Number 		= String("1").charCodeAt();
		public static const TWO:Number 		= String("2").charCodeAt();
		public static const THREE:Number	= String("3").charCodeAt();
		public static const FOUR:Number 	= String("4").charCodeAt();
		public static const FIVE:Number 	= String("5").charCodeAt();
		public static const SIX:Number 		= String("6").charCodeAt();
		public static const SEVEN:Number 	= String("7").charCodeAt();
		public static const EIGHT:Number 	= String("8").charCodeAt();
		public static const NINE:Number 	= String("9").charCodeAt();
		
		public static const PLUS:Number 	= String("+").charCodeAt();
		public static const MINUS:Number 	= String("-").charCodeAt();
		public static const MULTIPLY:Number = String("·").charCodeAt();
		public static const MULTIPLY_A:Number= String("*").charCodeAt();
		public static const MULTIPLY_X:Number= String("×").charCodeAt();
		public static const DIVIDE:Number 	= String(":").charCodeAt();
		public static const DIVIDE_S:Number 	= String("/").charCodeAt();
		public static const DIVIDE_G:Number 	= String("÷").charCodeAt();
		
		public static const EQUALS:Number 			= String("=").charCodeAt();
		public static const APPROXIMATION:Number	= String("≈").charCodeAt();
		public static const NOT_EQUALS:Number 		= String("≠").charCodeAt();
		public static const LESS:Number 			= String("<").charCodeAt();
		public static const GREATER:Number 			= String(">").charCodeAt();
		public static const LESS_OR_EQUALS:Number 	= String("≤").charCodeAt();
		public static const GREATER_OR_EQUALS:Number= String("≥").charCodeAt();
		
		public static const DOT:Number 				= String(".").charCodeAt();
		public static const COMMA:Number 			= String(",").charCodeAt();
		
		public static const BRACKET_OPEN:Number 	= String("(").charCodeAt();
		public static const BRACKET_CLOSE:Number 	= String(")").charCodeAt();
		
		public static const SPECIAL_FRACTION:Number = String("|").charCodeAt();
		
		


	}
}